package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.MedicinePriceDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDetailDAO;
import cn.edu.scau.cmi.hombio.dao.SupplierDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.MedicinePrice;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineStorageDetail;
import cn.edu.scau.cmi.hombio.domain.Supplier;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for MedicineStorage entities
 * 
 */

@Service("MedicineStorageService")

@Transactional
public class MedicineStorageServiceImpl implements MedicineStorageService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceDAO medicinePriceDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorageDetail entities
	 * 
	 */
	@Autowired
	private MedicineStorageDetailDAO medicineStorageDetailDAO;

	/**
	 * DAO injected by Spring that manages Supplier entities
	 * 
	 */
	@Autowired
	private SupplierDAO supplierDAO;

	/**
	 * Instantiates a new MedicineStorageServiceImpl.
	 *
	 */
	public MedicineStorageServiceImpl() {
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public MedicineStorage deleteMedicineStorageEmployee(Integer medicinestorage_id, Integer related_employee_id) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		medicinestorage.setEmployee(null);
		related_employee.getMedicineStorages().remove(medicinestorage);
		medicinestorage = medicineStorageDAO.store(medicinestorage);
		medicineStorageDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return medicinestorage;
	}

	/**
	 * Return all MedicineStorage entity
	 * 
	 */
	@Transactional
	public List<MedicineStorage> findAllMedicineStorages(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<MedicineStorage>(medicineStorageDAO.findAllMedicineStorages(startResult, maxRows));
	}

	/**
	 * Load an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public Set<MedicineStorage> loadMedicineStorages() {
		return medicineStorageDAO.findAllMedicineStorages();
	}

	/**
	 * Save an existing MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public MedicineStorage saveMedicineStorageMedicineStorageDetails(Integer id, MedicineStorageDetail related_medicinestoragedetails) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(id, -1, -1);
		MedicineStorageDetail existingmedicineStorageDetails = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(related_medicinestoragedetails.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineStorageDetails != null) {
			existingmedicineStorageDetails.setId(related_medicinestoragedetails.getId());
			existingmedicineStorageDetails.setTraceCode(related_medicinestoragedetails.getTraceCode());
			existingmedicineStorageDetails.setIsPutaway(related_medicinestoragedetails.getIsPutaway());
			existingmedicineStorageDetails.setIsUseout(related_medicinestoragedetails.getIsUseout());
			related_medicinestoragedetails = existingmedicineStorageDetails;
		}

		related_medicinestoragedetails.setMedicineStorage(medicinestorage);
		medicinestorage.getMedicineStorageDetails().add(related_medicinestoragedetails);
		related_medicinestoragedetails = medicineStorageDetailDAO.store(related_medicinestoragedetails);
		medicineStorageDetailDAO.flush();

		medicinestorage = medicineStorageDAO.store(medicinestorage);
		medicineStorageDAO.flush();

		return medicinestorage;
	}

	/**
	 * Delete an existing Supplier entity
	 * 
	 */
	@Transactional
	public MedicineStorage deleteMedicineStorageSupplier(Integer medicinestorage_id, Integer related_supplier_id) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id, -1, -1);
		Supplier related_supplier = supplierDAO.findSupplierByPrimaryKey(related_supplier_id, -1, -1);

		medicinestorage.setSupplier(null);
		related_supplier.getMedicineStorages().remove(medicinestorage);
		medicinestorage = medicineStorageDAO.store(medicinestorage);
		medicineStorageDAO.flush();

		related_supplier = supplierDAO.store(related_supplier);
		supplierDAO.flush();

		supplierDAO.remove(related_supplier);
		supplierDAO.flush();

		return medicinestorage;
	}

	/**
	 */
	@Transactional
	public MedicineStorage findMedicineStorageByPrimaryKey(Integer id) {
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(id);
	}

	/**
	 * Return a count of all MedicineStorage entity
	 * 
	 */
	@Transactional
	public Integer countMedicineStorages() {
		return ((Long) medicineStorageDAO.createQuerySingleResult("select count(o) from MedicineStorage o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public MedicineStorage deleteMedicineStorageMedicinePrices(Integer medicinestorage_id, Integer related_medicineprices_id) {
		MedicinePrice related_medicineprices = medicinePriceDAO.findMedicinePriceByPrimaryKey(related_medicineprices_id, -1, -1);

		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id, -1, -1);

		related_medicineprices.setMedicineStorage(null);
		medicinestorage.getMedicinePrices().remove(related_medicineprices);

		medicinePriceDAO.remove(related_medicineprices);
		medicinePriceDAO.flush();

		return medicinestorage;
	}

	/**
	 * Delete an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public void deleteMedicineStorage(MedicineStorage medicinestorage) {
		medicineStorageDAO.remove(medicinestorage);
		medicineStorageDAO.flush();
	}

	/**
	 * Save an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public MedicineStorage saveMedicineStorageMedicinePrices(Integer id, MedicinePrice related_medicineprices) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(id, -1, -1);
		MedicinePrice existingmedicinePrices = medicinePriceDAO.findMedicinePriceByPrimaryKey(related_medicineprices.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicinePrices != null) {
			existingmedicinePrices.setId(related_medicineprices.getId());
			existingmedicinePrices.setPrice(related_medicineprices.getPrice());
			related_medicineprices = existingmedicinePrices;
		} else {
			related_medicineprices = medicinePriceDAO.store(related_medicineprices);
			medicinePriceDAO.flush();
		}

		related_medicineprices.setMedicineStorage(medicinestorage);
		medicinestorage.getMedicinePrices().add(related_medicineprices);
		related_medicineprices = medicinePriceDAO.store(related_medicineprices);
		medicinePriceDAO.flush();

		medicinestorage = medicineStorageDAO.store(medicinestorage);
		medicineStorageDAO.flush();

		return medicinestorage;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public MedicineStorage saveMedicineStorageEmployee(Integer id, Employee related_employee) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		medicinestorage.setEmployee(related_employee);
		related_employee.getMedicineStorages().add(medicinestorage);
		medicinestorage = medicineStorageDAO.store(medicinestorage);
		medicineStorageDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return medicinestorage;
	}

	/**
	 * Save an existing Supplier entity
	 * 
	 */
	@Transactional
	public MedicineStorage saveMedicineStorageSupplier(Integer id, Supplier related_supplier) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(id, -1, -1);
		Supplier existingsupplier = supplierDAO.findSupplierByPrimaryKey(related_supplier.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsupplier != null) {
			existingsupplier.setId(related_supplier.getId());
			existingsupplier.setName(related_supplier.getName());
			related_supplier = existingsupplier;
		} else {
			related_supplier = supplierDAO.store(related_supplier);
			supplierDAO.flush();
		}

		medicinestorage.setSupplier(related_supplier);
		related_supplier.getMedicineStorages().add(medicinestorage);
		medicinestorage = medicineStorageDAO.store(medicinestorage);
		medicineStorageDAO.flush();

		related_supplier = supplierDAO.store(related_supplier);
		supplierDAO.flush();

		return medicinestorage;
	}

	/**
	 * Save an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public void saveMedicineStorage(MedicineStorage medicinestorage) {
		MedicineStorage existingMedicineStorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());

		if (existingMedicineStorage != null) {
			if (existingMedicineStorage != medicinestorage) {
				existingMedicineStorage.setId(medicinestorage.getId());
				existingMedicineStorage.setTime(medicinestorage.getTime());
			}
			medicinestorage = medicineStorageDAO.store(existingMedicineStorage);
		} else {
			medicinestorage = medicineStorageDAO.store(medicinestorage);
		}
		medicineStorageDAO.flush();
	}

	/**
	 * Delete an existing MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public MedicineStorage deleteMedicineStorageMedicineStorageDetails(Integer medicinestorage_id, Integer related_medicinestoragedetails_id) {
		MedicineStorageDetail related_medicinestoragedetails = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(related_medicinestoragedetails_id, -1, -1);

		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage_id, -1, -1);

		related_medicinestoragedetails.setMedicineStorage(null);
		medicinestorage.getMedicineStorageDetails().remove(related_medicinestoragedetails);

		medicineStorageDetailDAO.remove(related_medicinestoragedetails);
		medicineStorageDetailDAO.flush();

		return medicinestorage;
	}
}
